package com.qc.bug.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.qc.bug.pojo.BugInfo;
import com.qc.bug.pojo.UserInfo;
import com.qc.bug.services.bugInfo.BugInfoServices;
import com.qc.bug.util.MyCons;

@Controller
@RequestMapping(value = "/bugInfo")
public class BugInfoController {

	@Autowired
	private BugInfoServices bugInfoBiz;

	/**
	 * @author admin
	 * @param bugInfo
	 * @param session
	 * @return String
	 * @time 2017 下午3:48:02
	 * @desc 添加bug信息
	 * 
	 */
	@RequestMapping(value = "/addBugInfo")
	public String addBugInfo(BugInfo bugInfo, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(MyCons.USRE_LOGIN);
		bugInfo.setAddpersonid(userInfo.getUserid());

		bugInfoBiz.addBugInfo(bugInfo);

		return "user_back/index";
	}

	/**
	 * 
	 * @author admin
	 * @return String
	 * @time 2017 下午4:32:44
	 * @desc 根据bug描述查询类是的bug解决方案
	 * 
	 */
	@RequestMapping(value = "/description")
	public String findDescription(@RequestParam String description, @RequestParam(defaultValue = "1") int indexPage, Map<String, Object> map) {
		try {
			description = new String(description.getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		map.put("list", bugInfoBiz.findDescription(description, 1, 20));

		return "list";
	}

	@RequestMapping(value = "/findById/{bugId}")
	public String findById(@PathVariable("bugId") int bugId,Map<String, Object> map) {
		
		map.put("bugInfo", bugInfoBiz.findById(bugId));
		
		return "detailed";
	}

	@ResponseBody
	@RequestMapping(value = "/ajaxBug/{str}", produces = "text/html;charset=utf-8")
	public Object findAjaxBug(@PathVariable("str") String str) {

		List<Map<String, Object>> list = bugInfoBiz.findAjaxBug(str);
		return JSON.toJSONString(list);
	}

}
